package com.chinacoal.hr.levelimpl.client;

import com.chinacoal.hr.levelapi.api.CapUserClient;
import com.chinacoal.hr.levelapi.api.CcmsUserClient;
import com.chinacoal.hr.levelapi.api.CoframeUserLoginClient;
import com.chinacoal.hr.levelapi.entity.*;
import com.chinacoal.hr.levelimpl.service.EmployeeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author: YunTao.Li
 * @create: 2019-12-17 10:00
 * @description:
 **/
@Component
public class LoginUser {

    @Autowired
    private CoframeUserLoginClient coframeClient;

    @Autowired
    private CcmsUserClient userClient;

    @Autowired
    private CapUserClient capUserClient;

    @Autowired
    private EmployeeService employeeService;

    public Map getLoginUser() {
        Map iamUser = new HashMap();
        String empCode = "";
        String loginName = "";

        loginName = "10029417"; // 刘佳;10000345;郭向辉;10049269；刘国胜
//        loginName = "10010786";//曹丁；10010783；杨振宇；
//        10026203；姚莉荣；10011782;魏敏杰
//        step1 : 调用coframe验证头信息里的用户权限
//        Map loginUser = this.coframeClient.getLoginUser();
//        if ((loginUser == null) || (StringUtils.isBlank((CharSequence) loginUser.get("name")))) {
//            System.out.println("response of call coframe login return is null");
//            return null;
//        }
//        loginName = (String) loginUser.get("name");
        empCode = loginName;

        try {
            Employee employee = employeeService.getEmployeeDetail(empCode);
            iamUser.put("empCode", empCode);
            iamUser.put("empName", employee.getEmpName());
            iamUser.put("deptCode", employee.getDeptCode());
            iamUser.put("deptName", employee.getDeptName());
            iamUser.put("orgCode", employee.getOrgCode());
            iamUser.put("orgName", employee.getOrgName());
        } catch (Throwable e) {
            e.printStackTrace();
        }
//        // step2 : 根据用户登陆名获得用户详情(新)
//        try {
//            Result<EmployeeDetailVO> empDetailResult = capUserClient.getEmpDetail(null, loginName);
//            System.out.println("call capUserClient.getEmpDetail loginName :" + loginName + " empResult code : " + empDetailResult.getCode() + " msg:" + empDetailResult.getMsg() + " data:" + empDetailResult.getData());
//            EmployeeDetailVO empVO = empDetailResult.getData();
//            if (empDetailResult.getCode() == CodeMsg.Success.getCode()) {
//                empCode = empVO.getEmpCode();
//                String empName = empVO.getEmpName();
//
//                // 如果当前登陆人有部门，则默认取第一个
//                if (empVO.getOrgs() != null) {
//                    OrgOrganization org = empVO.getOrgs().get(0);
//                    iamUser.put("deptCode", org.getOrgCode());
//                    iamUser.put("deptName", org.getOrgName());
//                }
//                iamUser.put("empCode", empCode);
//                iamUser.put("empName", empName);
//
//
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            return null;
//        }
//
//        try {
//            Result<List<EmployeeVO>> empsResult = capUserClient.findEmpByEmpCode(empCode);
//            System.out.println("call capUserClient.findEmpByEmpCode empCode :" + empCode + " empResult code : " + empsResult.getCode() + " msg:" + empsResult.getMsg() + " data:" + empsResult.getData());
//            EmployeeVO emp = empsResult.getData().get(0);
//            System.out.println(emp.toString());
//            iamUser.put("orgCode", emp.getManagerOrgCode());
//            iamUser.put("orgName", emp.getManagerOrgName());
//        } catch (Exception e) {
//        }
        return iamUser;
    }
}
